from django.test import TestCase
from zxy.models import PubName, Book


class ExampleTest(TestCase):
    def setUp(self):
        print('我负责测试环境初始化')
        self.pub1 = PubName.objects.create(pubname="编程帮出版")

    # 基础功能测试
    def test_addition(self):
        def addition(x, y):
            return x + y

        # 断言函数加和运算
        self.assertEqual(addition(1, 1), 2, 'ass is failed')

    # 模型测试
    def test_model(self):
        # 创建pubname实例，
        pub1 = PubName.objects.create(pubname="程序帮出版社")
        book = Book.objects.create(title='Servlet', price='35.00', retail_price='35.00', pub=pub1)
        self.assertTrue(book is not None)
        # 使用断言判断
        self.assertNotEqual(Book.objects.count(), 2)
        self.assertEqual(Book.objects.count(), 1)

    # 视图层测试
    def test_view(self):
        pub1 = PubName.objects.create(pubname="机械工业出版社")
        book = Book.objects.create(title='Jsp', price='25.00', retail_price='25.00', pub=pub1)
        response = self.client.get('/update_book/%d/' % book.id)
        response['X-Token'] = 'C'  # 自定义响应头
        self.assertNotEqual(response.status_code, 200)
        self.assertEqual(response['X-Token'], 'C', 'it is not same')

    def tearDown(self):
        print('我负责清理测试环境')